﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TextMining;
using Microsoft.Win32;

namespace BlogMining
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        BlogReader reader = null;
        WordsSet set = null;

        public Window1()
        {
            InitializeComponent();
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;

            try
            {
                double percentage = GetPercentage();
                if (reader == null)
                {
                    if (BeginRead() == false)
                    {
                        Cursor = Cursors.Arrow;
                        return;
                    }
                }
                FillMyMethodWords(percentage);
                FillTfIdfWords(percentage);
                ReportStatus();
            }
            catch (Exception exception)
            {
                MessageBox.Show("Error occured: " + exception.Message);
                Cursor = Cursors.Arrow;
            }

            Cursor = Cursors.Arrow;
        }

        private void ReportStatus()
        {
            int differences = RetrieveDifferences();
            ResultLabel.Content = differences + " difference(s) found";
        }

        private int RetrieveDifferences()
        {
            int differences = 0;

            foreach (Word word in MyMethodListBox.Items)
                if (TfIdfListBox.Items.Contains(word) == false)
                    differences++;
            return differences;
        }

        private bool BeginRead()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Blog archives (*.txt)|*.txt";
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog().Value == false)
                return false;

            Cursor = Cursors.Wait;
            reader = new BlogReader();
            set = new WordsSet();

            reader.ReadStopWords("stop_words.txt");
            reader.ReadBlog(dialog.FileName, set);

            return true;
        }

        private void FillTfIdfWords(double percentage)
        {
            TfIdfListBox.DataContext = set.TfIdfLessImportant(reader.Documents, percentage);
        }

        private void FillMyMethodWords(double percentage)
        {
            MyMethodListBox.DataContext = set.OurMethodLessImportant(percentage);
        }

        private double GetPercentage()
        {
            double percentage;

            if (PercentageTextBox.Text == String.Empty)
                throw new Exception("No percentage entered");
            percentage = Double.Parse(PercentageTextBox.Text);
            if (percentage < 0 || percentage > 100)
                throw new Exception("Invalid percentage range");
            return percentage;
        }
    }
}
